let tableIns;
let tree;
layui.use(['element','upload', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate,upload = layui.upload;
    let $ = layui.jquery;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;



    tableIns = table.render({
        elem: '#sellerTable'
       // , url: ctx + '/seller/sellerlist/page'
        , url: '/sellerconfig/configlist/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: '#sellerTableToolbar'
        , title: '商户服务类型列表'
        , cols: [[
             {field: 'configValue',align: 'center', title: '商户服务类型 '}
            , {field: 'right', title: '操作', toolbar: '#sellerTableBar'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });



    //头工具栏事件
    table.on('toolbar(display)', function (obj) {
        switch (obj.event) {
            case 'addData':
            	layer.open({
                    type: 2,
                    title: '新增商户服务类型',
                    closeBtn: 1, //显示关闭按钮
                    anim: 2,
                    area: ['800px', '450px'],
                    shadeClose: true, //开启遮罩关闭
                    content: '/sellerconfig/configinsert'
                });
                break;
        }
    });

    //监听行工具事件
    table.on('tool(display)', function (obj) {
        let data = obj.data;
        //删除
        if (obj.event === 'del') {
        	window.remove(data.configName);
        }
        //编辑
        else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: '编辑商户',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['800px', '450px'],
                shadeClose: true, //开启遮罩关闭
                content: '/sellerconfig/findByName?id='+data.configName,
                success: function(layero, index){

                }
            });
        }
    });
    
    
    window.remove = function(ids){
        layer.confirm('确定要删除该类型吗', {icon: 3, title:'提示'}, function(data){
        	console.log(data);
            $.ajax({
                url: "/sellerconfig/deleteByName?id=" + ids ,
                dataType:'json',
                type:'POST',
                success:function(data){
                	layer.alert(data);
                },error:function(data){
                	layer.alert(data.responseText, {icon: 1,closeBtn: 0 },
                			function(){
                	window.location.reload();
                	});
                }
            })
        });
    }
    

    form.on('submit(menu-query)', function (data) {
    	
     	 // 搜索条件
         var configValue=data.field.configValue;
         	// var tel=$("#tel").val();
             table.reload('sellerTable', {
                 url:'/sellerconfig/configlist/page'
                 , where: {
                     'configValue':configValue,
                 }
                 , page: {
                     curr: 1
                 }
             });
             return false;
         
    });
    
    /* 监听提交 */
    form.on('submit(user-savess)', function(data){
    	  $.ajax({
            // 几个参数需要注意一下
            type: "POST",// 方法类型
            url: '/sellerconfig/save',
            data: data.field,
            success: function (data) {
            	layer.alert(data, {icon: 1,closeBtn: 0 },
            			function(){
            		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    	        	parent.layer.close(index); //再执行关闭
    	        	window.parent.$("#qingkong").click();
            	});
            	
            },
            error : function() {
            	layer.alert("异常！");
            }
        });
    });
    
    

});